package cn.sinata.wanglin.village.activity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.hyphenate.easeui.domain.EaseEmojicon;
import com.hyphenate.easeui.domain.EaseEmojiconGroupEntity;
import com.hyphenate.easeui.model.EaseDefaultEmojiconDatas;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenu;
import com.hyphenate.easeui.widget.emojicon.EaseEmojiconMenuBase;
import com.xilada.xldutils.activitys.DialogActivity;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;

public class EditPubWordActivity extends DialogActivity {


    @BindView(R.id.ease_layout_emojicon_menu)
    EaseEmojiconMenu emojiconMenu;
    @BindView(R.id.et_comment)
    EditText et_comment;
    @BindView(R.id.expressionView)
    TextView expressionView;
    @BindView(R.id.tv_action)
    TextView tv_action;
    @BindView(R.id.cb_isanonymous)
    CheckBox cb_isanonymous;
    @Override
    protected int setContentLayout() {
        return R.layout.activity_edit_pub_word;
    }

    @Override
    protected void initView() {
        getWindow().setGravity(Gravity.BOTTOM);


        String content  = getIntent().getStringExtra("content");
        if(!TextUtils.isEmpty(content)) {
            et_comment.setText(EaseSmileUtils.getSmiledText(mContext, content));
        }
        List<EaseEmojiconGroupEntity> emojiconGroupList = new ArrayList<>();
        emojiconGroupList.add(new EaseEmojiconGroupEntity(R.drawable.ee_1,  Arrays.asList(EaseDefaultEmojiconDatas.getData())));
        emojiconMenu.init(emojiconGroupList);
        emojiconMenu.setEmojiconMenuListener(new EaseEmojiconMenuBase.EaseEmojiconMenuListener() {
            @Override
            public void onExpressionClicked(EaseEmojicon emojicon) {
                int temp=et_comment.getSelectionStart();
                Editable mEditable=et_comment.getEditableText();
                if(temp<0||temp>=mEditable.length()){
                    mEditable.append(EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
                }else{
                    mEditable.insert(temp,EaseSmileUtils.getSmiledText(mContext,emojicon.getEmojiText()));
                }
            }

            @Override
            public void onDeleteImageClicked() {
                if (!TextUtils.isEmpty(et_comment.getText())) {
                    KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                    et_comment.dispatchKeyEvent(event);
                }
            }
        });
    }

    @OnTextChanged(R.id.et_comment)
    void onCommentChanged(Editable s){
        if (TextUtils.isEmpty(s)) {
            tv_action.setVisibility(View.GONE);
        } else {
            tv_action.setVisibility(View.VISIBLE);
        }
    }

    void addComment(){
        String content = et_comment.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            showToast("编辑内容不能为空");
            return;
        }
        Intent intent=new Intent();
        intent.putExtra("content",content);
        setResult(RESULT_OK,intent);
        finish();
    }

    @OnClick({R.id.expressionView,R.id.et_comment,R.id.tv_action,R.id.iv_finish_comment})
    void onViewClick(View v) {
        switch (v.getId()) {
            case R.id.expressionView:
                Utils.hideSoftWindow(this,v);
                if (emojiconMenu.getVisibility() == View.VISIBLE) {
                    emojiconMenu.setVisibility(View.GONE);
                } else {
                    emojiconMenu.setVisibility(View.VISIBLE);
                    et_comment.requestFocus();
                }
                break;
            case R.id.et_comment:
                emojiconMenu.setVisibility(View.GONE);
                break;
            case R.id.tv_action:
                addComment();
                break;
            case R.id.iv_finish_comment:
                finish();
                break;
        }
    }
}

